#!/bin/bash

set -e

CURR_PATH=$(cd `dirname $0`; pwd)
LOG_PATH="/var/log/tool-box"
PID_PATH="/var/pid/tool-box"

mkdir -p ${LOG_PATH}
mkdir -p ${PID_PATH}

MANAGERS="config-center service-center gateway-center admin-center"
SERVICES="base-service data-service docker-service automation-service"
ALL_SERVICE="${MANAGERS} ${SERVICES}"

function usage() {
    echo ""
    echo "Usage: $0 <OPERATION> [SERVICES...]"
    echo ""
    echo "Param 'OPERATION': start stop help"
    echo "Param 'SERVICE': ${ALL_SERVICE}"
    echo ""
    echo "Examples:"
    echo ""
    echo "  To start all service, you can execute command like this:"
    echo "      '$0 start' or '$0 start all' or '$0 start ALL'"
    echo "  To start a service, you can execute command like this:"
    echo "      '$0 start config-center'"
    echo ""
    echo "  To stop all service, you can execute command like this:"
    echo "      '$0 stop' or '$0 stop all' or '$0 stop ALL'"
    echo "  To stop a service, you can execute command like this:"
    echo "      '$0 stop config-center'"
    echo ""
    echo "  To restart all service, you can execute command like this:"
    echo "      '$0 restart' or '$0 restart all' or '$0 restart ALL'"
    echo "  To restart a service, you can execute command like this:"
    echo "      '$0 restart config-center'"
    echo ""
    echo "  To update services, you can execute command like this:"
    echo "      '$0 update'"
    echo ""
    echo "  To get help info, you can execute command like this:"
    echo "      '$0 help'"
}

function up() {
    mkdir -p ${PID_PATH}
    touch ${LOG_PATH}/$2.log
    nohup java -jar $1 --logback.file.path=${LOG_PATH} > /dev/null 2>&1 &
    echo $! > ${PID_PATH}/$2.pid
    tail -f ${LOG_PATH}/$2.log
}

function start() {
    if [[ $1 == "" ]] || [[ $1 == "ALL" ]] || [[ $1 == "all" ]]; then
        for SERVICE in ${ALL_SERVICE}
        do
            start ${SERVICE}
        done
    elif [[ $(echo ${MANAGERS[@]} | grep $1) != "" ]]; then
        up ${CURR_PATH}/manager/$1/target/$1-1.0.0-SNAPSHOT.jar $1
    elif [[ $(echo ${SERVICES[@]} | grep $1) != "" ]]; then
        up ${CURR_PATH}/service/$1/target/$1-1.0.0-SNAPSHOT.jar $1
    else
        echo "Param Error!!!!! Please input $0 help for more info!!!!!"
    fi
}

function stop() {
    if [[ $1 == "" ]] || [[ $1 == "ALL" ]] || [[ $1 == "all" ]]; then
        for SERVICE in ${ALL_SERVICE}
        do
            stop ${SERVICE}
        done
    elif [[ $(echo ${ALL_SERVICE[@]} | grep $1) != "" ]]; then
        if [[ -f ${PID_PATH}/$1.pid ]]; then
            kill $(cat ${PID_PATH}/$1.pid)
            rm -f cat ${PID_PATH}/$1.pid
        fi
    else
        echo "Param Error!!!!! Please input $0 help for more info!!!!!"          
    fi
}

function restart() {
    stop $1
    start $1
}

function update() {
    git --git-dir=${CURR_PATH}/.git --work-tree=${CURR_PATH}/ pull
    mvn -f ${CURR_PATH}/pom.xml clean package -Dmaven.test.skip=true
}

if [[ $1 == "help" ]]; then
    usage
elif [[ $1 == "start" ]]; then
    start $2
elif [[ $1 == "stop" ]]; then
    stop $2
elif [[ $1 == "restart" ]]; then
    restart $2
elif [[ $1 == "update" ]]; then
    update
else
    echo "Param Error!!!!! Please input $0 help for more info!!!!!"
fi